//
//  UnFinishedOrderFootView.m
//  FMJY
//
//  Created by 艾玩科技 on 17/4/13.
//  Copyright © 2017年 随心. All rights reserved.
//

#import "UnFinishedOrderFootView.h"

@implementation UnFinishedOrderFootView
{
    UIScrollView*   _scrollView;
   
}
-(id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        [self addSubview:_scrollView];
       
        
        _scrollView.pagingEnabled = YES;
        _scrollView.bounces = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        
        _scrollView.backgroundColor =[UIColor colorWithRed:0.933 green:0.933 blue:0.933 alpha:1.00];
       
        
       

        
    }
    
    return self;
}
-(void)setDriverIDdArr:(NSArray *)driverIDdArr
{
    _driverIDdArr = driverIDdArr;
}
-(void)setDriverNameArr:(NSArray *)driverNameArr
{
    _driverNameArr = driverNameArr;
    
     _scrollView.contentSize = CGSizeMake(KScreenWidth * _driverNameArr.count, self.bounds.size.height);
    
    for (int i = 0; i <_driverNameArr.count; i ++) {
        
        //添加子视图
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake( 30 +i * KScreenWidth , 20, KScreenWidth- 30*2 , 268)];
        view1.backgroundColor = [UIColor whiteColor];
        view1.layer.cornerRadius = 5;
        view1.layer.masksToBounds =YES;
        //司机姓名
        UILabel *driceNLa = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, view1.width, 40)];
        driceNLa.textAlignment = NSTextAlignmentCenter;
        driceNLa.text = _driverNameArr[i];
        [view1 addSubview:driceNLa];
        
        
        //备注
        self.markTextViewM = [[UITextView alloc] initWithFrame:CGRectMake(0, driceNLa.bottom, view1.width, 150)];
        self.markTextViewM.backgroundColor = [UIColor colorWithRed:1.000 green:0.000 blue:0.475 alpha:1.00];
        self.markTextViewM.delegate = self;
        [self.markTextViewM  textFiledHiddenkeyBord1];
        self.markTextViewM.text = @"备注";
        [view1 addSubview:self.markTextViewM];
        
        
        //改派按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake((view1.width - 80)/2.0, self.markTextViewM.bottom +20, 80, 40);
        btn.backgroundColor = [UIColor colorWithRed:1.000 green:0.000 blue:0.475 alpha:1.00];
        [btn setTitle:@"改派给他" forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(changeDricerAct:) forControlEvents:UIControlEventTouchUpInside];
        [view1 addSubview:btn];
        
        [_scrollView addSubview:view1];
    }
   
}

//textView开始编辑时候的代理方法
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"备注"]) {
        
        textView.text = @"";
        textView.textColor = [UIColor blackColor];
    }
    
}
//textView结束编辑时候的代理方法
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length < 1)
        
    {
        textView.text = @"备注";
        textView.textColor = [UIColor grayColor];
    }
    
    
}
-(void)changeDricerAct:(UIButton *)sender
{
    
    NSString *driverID =[NSString stringWithFormat:@"%@",_driverIDdArr[sender.tag]] ;
    
    

    if (self.markTextViewM.text.length == 0 ) {
//        || [self.markTextViewM.text isEqualToString:@"备注"]
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"备注不能为空，请重新输入" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
        return;
        
      
    }
#pragma mark -- 改派司机 --
    [CoreWork changeDriverWithorderNo:self.orderNumStrA orderMark:self.markTextViewM.text  loginToken:GetUserLoginToken userDriverId:driverID Block:^(id responsObject) {
        #pragma mark -- 服务器报错  --
        if (![responsObject isKindOfClass:[NSDictionary class]]) {

            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"服务器开小差了，请稍后重试" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
            return;
           
            
        }
        
#pragma mark -- 改派失败（订单不存在或者已超时） --
        if (([responsObject isKindOfClass:[NSDictionary class]] ) && ([[responsObject objectForKey:@"status"] integerValue ] == 0) ) {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@", [responsObject objectForKey:@"message"]] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
            return;
        }

#pragma mark -- 改派成功 --
        if (([responsObject isKindOfClass:[NSDictionary class]] ) && ([[responsObject objectForKey:@"status"] integerValue ] == 1) ) {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@", [responsObject objectForKey:@"message"]] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            [alert show];
            
            if (self.changeOverDelegateA && ([self.changeOverDelegateA respondsToSelector:@selector(changeOverDelegateA)])) {
                [self.changeOverDelegateA backToSuper];
            }
//            return;
        }
        
    }];
}



@end
